package vg.modules.filter;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.graph_view_manager.interfaces.tab.ATab;
import vg.services.plugin_manager.event_and_request.event.AUIEvent;
import vg.services.plugin_manager.event_and_request.event.UIEventChangeTab;
import vg.services.plugin_manager.interfaces.IPlugin;
import vg.services.plugin_manager.realization.PluginParameter;
import vg.services.user_interface_manager.interfaces.UserInterfaceObserverAdapter;

/**
 * This class connected filter's plugin.
 *
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class FilterPlugin implements IPlugin {
	// Main data
	private AGraphView view;
	private FilterPanel filter;

    // Mutex
    private final Object generalMutex= new Object();

    @Override
	public void install(final PluginParameter pluginParameter) throws Exception {
		this.filter = new FilterPanel("Filter", pluginParameter);
		JMenuItem jMenuItem = new JMenuItem("Filter");
		jMenuItem.setIcon(new ImageIcon("./data/resources/textures/filter.png"));
		jMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (generalMutex) {
                    filter.open();
                    filter.changeView(view);
                }
			}
		});
		jMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_DOWN_MASK));
        pluginParameter.userInterfaceManager.addMenuItem(jMenuItem, "edit");

        pluginParameter.userInterfaceManager.addObserver(new UserInterfaceObserverAdapter() {
            @Override
            public void dispatchEvent(AUIEvent event) {
                switch(event.getType()) {
                    case DEF_CHANGE_TAB: {
                        UIEventChangeTab bufEvent = (UIEventChangeTab)event;
                        ATab tab = bufEvent.getTab();
                        if (tab != null) {
                            switch (tab.getTabType()) {
                                case ATab.DEF_GRAPH_VIEW_TAB_TYPE: {
                                    synchronized (generalMutex) {
                                        view = (AGraphView)tab;
                                        if(filter.isVisible()) {
                                            filter.changeView(view);
                                        }
                                    }
                                    break;
                                }

                                default: {
                                    synchronized (generalMutex) {
                                        view = null;
                                        if(filter.isVisible()) {
                                            filter.changeView(view);
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                        break;
                    }
                    case DEF_CLOSE_PROGRAM: {
                        filter.dispose();
                        break;
                    }
                    case DEF_CHANGE_UI_STYLE: {
                        filter.updateUITheme();
                        break;
                    }
                }
            }
        });

		filter.updateUITheme();
	}
}
